package itsm.isperp.module.controller.app;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.app.AppMessageReceiver;
import itsm.isperp.module.entity.app.AppMessageSuccess;
import itsm.isperp.module.service.app.AppMessageReceiverService;
import itsm.isperp.module.service.app.AppMessageService;
import itsm.isperp.module.service.app.AppMessageSuccessService;
import itsm.isperp.module.service.app.AppUserService;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 系统帮助的控制器
 * 
 * @author lizx
 * @date 2013-7-27
 */
@Controller
@RequestMapping("app/message/receiver")
public class AppMessageReceiverController extends
		BaseController<AppMessageReceiver> {

	@Autowired
	protected AppMessageReceiverService appMessageReceiverService;

	@Autowired
	protected AppMessageService appMessageService;

	@Override
	public AppMessageReceiverService getService() {
		return appMessageReceiverService;
	}

	@Override
	public String getPageTitle() {
		return "站内消息";
	}

	/**
	 * 新建消息入口
	 */
	@RequestMapping("edit")
	@Override
	public ModelAndView edit(String id, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = new ModelAndView("app/message/receiver/edit");
		AppMessageReceiver model = new AppMessageReceiver();
		String title = req.getParameter("title");
		String sourceId = req.getParameter("sourceId");
		mv.addObject("sourceId", sourceId);
		mv.addObject("model", model);
		mv.addObject("title", title);
		mv.addObject("content", null);
		return this.getEdit(id);
	}

	@RequestMapping("save")
	@ResponseBody
	@Override
	public ResultMessage save(AppMessageReceiver model, HttpServletRequest req) {
		String messageType = req.getParameter("messageType");
		this.getService().sendTo(model, req.getParameter("title"),
				req.getParameter("content"), messageType);

		return ResultMessage.newInstance(true, null, model.getId());
	}

	@RequestMapping(value = "replyEdit")
	public ModelAndView replay(String messageId, HttpServletRequest req)
			throws Exception {
		ModelAndView mv = new ModelAndView("app/message/receiver/replyEdit");
		AppMessageReceiver model = new AppMessageReceiver();
		model.setId(HibernateUUIDGenerator.generate());

		AppMessageSuccessService appMessageSuccessService = ContextHolder
				.getSpringBean("appMessageSuccessService");
		AppMessageSuccess result = appMessageSuccessService
				.findOneByRequest(new JqGridRequest()
						.isPageable(false)
						.setSelectColumns("title", "sender_id", "message_type",
								"source_id")
						.addFilterRule("id", JqGridFilterRuleOp.eq, messageId));
		if (result != null) {

			mv.addObject("title", "回复：" + result.getTitle());
			model.setSourceId(result.getSourceId());
			appMessageSuccessService.markReadMessage(messageId);
			if (StringUtils.isNotEmpty(result.getSender())) {
				AppUserService appUserService = ContextHolder
						.getSpringBean("appUserService");

				IUser sender = appUserService
						.findByUsername(result.getSender());
				mv.addObject("sender", sender);
				model.setReceiverId(sender.getUsername());
			}
		}
		mv.addObject("send", result);
		mv.addObject("model", model);
		return mv;
	}

}
